﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class HarteMischung : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HarteMischung"/> class.
        /// </summary>
        public HarteMischung()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HarteMischung"/> class.
        /// </summary>
        /// <param name="_harteMischung">The _harte mischung.</param>
        internal HarteMischung(HarteMischung _harteMischung)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Processes the specified _bitmap source.
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            UnsafeBitmap bitmapSource = _bitmap1;
            UnsafeBitmap bitmapDest = _bitmap2;

            Color color = Color.Transparent;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;
            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    color = bitmapSource.GetPixel(x, y);

                    int nR = color.R;
                    int nG = color.G;
                    int nB = color.B;

                    if ((nR < 255 - nR) && (nG < 255 - nG) && (nB < 255 - nB))
                    {
                        nR = 0; nG = 0; nB = 0;
                    }
                    else if ((nR > 255 - nR) && (nG > 255 - nG) && (nB > 255 - nB))
                    {
                        nR = 255; nG = 255; nB = 255;
                    }
                    color = Color.FromArgb(nR, nG, nB);
                    bitmapDest.SetPixel(x, y, color);
                }
            }
            bitmapSource.Dispose();
            return _bitmap2;
        }

        #endregion

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HarteMischung(this);
        }
    }
}
